package com.zbf.web;

import com.zbf.common.ResponseResult;
import com.zbf.common.ResultStatus;
import com.zbf.qunxian.QuanXian;
import com.zbf.service.MenuService;
import com.zbf.utils.MyContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/2  11:22
 * 描述: 权限菜单信息
 */
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 作者: LCG
     * 日期: 2021/6/2  11:23
     * 描述: 获取用户拥有的权限菜单
     * @Param []
     * @Return com.zbf.common.ResponseResult
     */
    @RequestMapping("getMenuList")
    public ResponseResult getMenuList(HttpServletRequest request){

        String userId = MyContextUtils.getUserId(request);

        List<Map<String, Object>> userMenus = menuService.getUserMenus(userId);

        for(int i=userMenus.size()-1;i>=0;i--){
            if(userMenus.get(i).get("menuName").toString().equals("首页")){
                userMenus.remove(i);
            }
        }

        ResponseResult responseResult = ResponseResult.getResponseResult().setCode(ResultStatus.AUTH_SUCCESS.getCode())
                .setMsg(ResultStatus.AUTH_SUCCESS.getMsg())
                .setResult(userMenus);

        return responseResult;
    }

    @RequestMapping("getAllMenuList")
    public ResponseResult getAllMenuList(HttpServletRequest request){

        List<Map<String, Object>> userMenus = menuService.getAllUserMenus();

        ResponseResult responseResult = ResponseResult.getResponseResult().setCode(ResultStatus.AUTH_SUCCESS.getCode())
                .setMsg(ResultStatus.AUTH_SUCCESS.getMsg())
                .setResult(userMenus);

        return responseResult;
    }


    @RequestMapping("addMenu")
    public ResponseResult addMenu(@RequestBody Map<String,String> map){
        String menuNameKey=map.get("menuName");
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(menuNameKey, "");

        ResponseResult responseResult=null;
        if(aBoolean){
            redisTemplate.expire(menuNameKey,30,TimeUnit.SECONDS);
            String id= ""+(System.currentTimeMillis()+new Random().nextInt(10000));
            map.put("id",id);

            map.put("code",id);

            int i = menuService.addMenu(map);

            responseResult = ResponseResult.getResponseResult().setCode(ResultStatus.AUTH_SUCCESS.getCode())
                    .setMsg(ResultStatus.AUTH_SUCCESS.getMsg());
        }else{
            responseResult = ResponseResult.getResponseResult().setCode(ResultStatus.AUTH_FAIL_FORDID.getCode())
                    .setMsg(ResultStatus.AUTH_FAIL_FORDID.getMsg());

        }


        return responseResult;
    }


    @RequestMapping("toUpdateMenu")
    @QuanXian("menu:list:upddate")
    public ResponseResult toUpdateMenu(){

        ResponseResult responseResult = ResponseResult.getResponseResult().setCode(ResultStatus.AUTH_SUCCESS.getCode())
                .setMsg(ResultStatus.AUTH_SUCCESS.getMsg());

        return responseResult;

    }

}
